#ifndef QPROJECT_H
#define QPROJECT_H

#include "shared_global.h"
#include "qruntimedata.h"

#include <QObject>
#include <QSize>
#include <QStringList>
#include <QList>
#include <QMap>
#include <QColor>

class QXmlStreamReader;
class QDrivePluginInterface;
class QAbstractDrive;

enum enProjectChangedType
{
    PCT_NAME,
    PCT_NOTES,
    PCT_FIRST_PAGE,
    PCT_DESIGN_SIZE,
    PCT_RUNNING_SIZE,
    PCT_RESOURCE_FILE,
    PCT_RUNNING_DATA_MODIFY,
    PCT_RUNNING_DATA_ADD,
    PCT_RUNNING_DATA_DEL,
    PCT_PAGE_DATA_MODIFY,
    PCT_PAGE_DATA_ADD,
    PCT_PAGE_DATA_DEL,
    PCT_RESOURCE_DATA_MODIFY,
    PCT_RESOURCE_DATA_ADD,
    PCT_RESOURCE_DATA_DEL,
    PCT_BOTTOM
};

struct tagPageInfo
{
    QString     m_name;
    QString     m_type;
    QString     m_data;
};

struct tagResourctInfo
{
    QString     m_name;
    QByteArray  m_data;
};

struct tagDriveInfo
{
    QString         m_name;
    QString         m_path;
    QAbstractDrive  *m_drive;
};

struct tagProjectInfo
{
    QString     m_projectName;
    QString     m_projectNotes;
    QString     m_strFirstPage;
    QSize       m_designSize;
    QSize       m_runningSize;
    bool        m_needFrame;
    bool        m_couldMax;
    bool        m_couldMin;
    QColor      m_backgroundColor;
    QList<QRuntimeData*>        m_runtimeDataList;
    QList<tagPageInfo*>         m_pageList;
    QList<tagDriveInfo*>        m_drives;
    QMap<QString,tagResourctInfo*>      m_resourceList;
};



struct tagDrivePluginInfo
{
    QDrivePluginInterface  *m_drivePlugin;
    QString                 m_path;
};

class SHAREDSHARED_EXPORT QProject : public QObject
{
    Q_OBJECT
protected:
    explicit QProject();
    ~QProject();
public:
    static QProject*   project();
    static void        releaseProject();
    bool        open(const QString & path);
    bool        save(const QString & path);
    bool        save();
    void        newProject(const QString & path,const QString & name);


    void        setProjectName(const QString & name);
    QString     getProjectName();

    void        setProjectNotes(const QString & notes);
    QString     getProjectNotes();

    void        setFirstPage(const QString & page);
    QString     getFirstPage();

    void        setDesignSize(const QSize & size);
    QSize       getDesignSize();

    void        setRunningSize(const QSize &size);
    QSize       getRunningSize();

    void        setNeedFrame(bool need);
    bool        getNeedFrame();

    void        setCouldMax(bool could);
    bool        getCouldMax();

    void        setCouldMin(bool could);
    bool        getCouldMin();

    void        setBackColor(const QColor &color);
    QColor      getBackColor();

    QRuntimeData  getRuntimeData(int index);
    int         getRunningIndex(const QString &name);
    int         getRuntimeDataCount();
    void        moveRuntimeDataDown(int index);
    void        moveRuntimeDataUp(int index);
    void        setRuntimeData(int index,const QRuntimeData &info);
    void        removeRuntimeData(int index);
    void        insertRuntimeData(int index,const QRuntimeData &info);

    tagPageInfo  getPage(int index);
    tagPageInfo  getPage(const QString &page);
    int         getPageCount();
    void        setPage(int index,const tagPageInfo &info);
    void        removePage(int index);
    void        removePage(const QString &name);
    void        insertPage(int index,const tagPageInfo &info);

    tagResourctInfo  getResource(const QString &name);
    void        removeResource(QObject *obj,const QString &key);
    void        insertResource(const QString &name,QObject* obj,const QString &key);

    tagDriveInfo    getDrive(const QString &name);
    tagDriveInfo    getDrive(int index);
    int             getDriveIndex(const QString &name);
    int             getDriveCount();
    void        removeDrive(int index);
    void        insertDrive(int index,const tagDriveInfo &info);

    QString     getProjectPath();

    bool        durty();

    void        inserTempResource(const QString &name);
    void        removeTempResource(const QString &name);

    void        addDrivePlugin(const QString &path,QDrivePluginInterface * plugin);
    QMap<QString,tagDrivePluginInfo> drivePlugins();
public slots:
    bool        close();
    void        driveAttributeChanged();
signals:
    void    openProject();
    void    newProject();
    void    saveProject();
    void    beforeClosed();
    void    durtyChanged();
    void    changed(int type,int index=-1);
    QByteArray getResourceData(const QString &name);
public slots:

protected:
    void        readProject(QXmlStreamReader *r);
    QSize       readSize(QXmlStreamReader *r);
    void        readRuntime(QXmlStreamReader *r);
    void        readPage(QXmlStreamReader *r);
    void        readDrive(QXmlStreamReader *r);
    void        setDurty(bool durty);

    void        writeInt(QByteArray &array, int value);
    int         readInt(QByteArray &array);
protected:
    tagProjectInfo              m_projectInfo;
    QString                     m_projectPath;
    QString                     m_projectFileName;
    bool                        m_durty;
    QMap<QObject*,QMap<QString,QString> >    m_resourceUser;
    QMap<QString,int>                        m_resourceTimes;
    QMap<QString,uchar*>                     m_resourceData;
    QMap<QString,uchar*>                     m_tempResource;
    QMap<QString,tagDrivePluginInfo>         m_drives;

    static      QProject        *m_project;
};

#endif // QPROJECT_H
